var express = require('express')

var router = express.Router()

// 导入MySQL模
var mysql = require('mysql')

var mysqlConfig = require('../../config/mysql')

// 连接池
var pool = mysql.createPool(mysqlConfig)

//默认
router.get('/', function (req, res, next) {
    res.send('接口路径不完整')
    next()
});

//查询消息列表
router.get('/getMessageList', (req, res, next) => {
    pool.getConnection((err, connection) => {
        connection.query('SELECT * FROM message_list_table', (err, result) => {
            if (!err) {
                results = {
                    code: 200,
                    msg: '查询成功',
                    data: result
                };
                res.json(results)
            } else {
                res.json({
                    code: 500,
                    msg: '查询失败',
                    err
                });
            }
            connection.release()
        })
    })
})

//新增消息
router.get('/addMessage', (req, res, next) => {
    pool.getConnection((err, connection) => {

        //参数
        const {
            user_account,
            user_name,
            user_message,
            send_time
        } = req.query

        connection.query(`INSERT INTO message_list_table( user_account, user_name, user_message, send_time ) VALUES( "${user_account}", "${user_name}", "${user_message}", "${send_time}" )`, (err, result) => {
            if (!err) {
                results = {
                    code: 200,
                    msg: '发送成功',
                };
                res.json(results)
            } else {
                res.json({
                    code: 500,
                    msg: '发送失败',
                    err
                });
            }
            connection.release()
        })

    })
})

module.exports = router